<?php

if (!defined('UC_FILESYSTEM_ROOT')) 
{
    define('UC_FILESYSTEM_ROOT', dirname(__FILE__) . '/');
    require_once UC_FILESYSTEM_ROOT . 'UC_PHPFileSystemAutoloader.php';
}

class UC_PHPFileSystem
{
    public function __construct()
    {
        
    }
    
    public static function copy_file(
        $source, $destination, $overwrite = false)
    {
        
        $source_path = pathinfo($source);
        $destination_path = pathinfo($destination);
        
        if(is_file($source))
        {
            if(is_file($destination))
            {
                if($overwrite)
                {
                    set_error_handler(array('UC_PHPFileSystem', 'error_handler'));
                    copy($source, $destination);
                    restore_error_handler();
                }
                else
                {
                    throw new UC_PHPFileSystemException(
                        'The destination file already exists.');
                }
            }
            elseif(is_dir($destination))
            {
                if(is_file($destination . '/' . $source_path['basename']))
                {
                    if($overwrite)
                    {
                        copy($source, $destination . '/' . $source_path['basename']);
                    }
                    else
                    {
                        throw new UC_PHPFileSystemException(
                            'The destination file already exists.');
                    }
                }
                else 
                {
                    copy($source, $destination . '/' . $source_path['basename']);
                }
            }
            elseif(is_dir($destination_path['dirname']))
            {
                copy($source, $destination);
            }
        }
        elseif(is_dir($source_path['dirname']))
        {
            if(is_dir($destination))
            {
                $items = glob($source);
                foreach($items as $item)
                {
                    if(is_file($item))
                    {
                        if(is_file($destination . '/' . pathinfo($item, PATHINFO_BASENAME)))
                        {
                            if($overwrite)
                            {
                                copy($item, $destination . '/' . pathinfo($item, PATHINFO_BASENAME));
                            }
                            else
                            {
                                throw new UC_PHPFileSystemException(
                                    'The destination file already exists.');
                            }
                        }
                        else
                        {
                            copy($item, $destination . '/' . pathinfo($item, PATHINFO_BASENAME));
                        }
                    }
                }
            }
            else
            {
                throw new UC_PHPFileSystemException(
                    'Invalid destination path.');
            }
        }
    }
    
    public static function copy_directory($source, $destination, $overwrite = false)
    {
        
    }
    
    public static function create_directory($path)
    {
        
        if(!is_dir($path))
        {            
            if(is_dir(dirname($path)))
            {
                if(mkdir($path))
                {
                    return new UC_PHPDirectory($path);
                }
                else
                {
                    throw new UC_PHPFileSystemException(
                        'Unable to create directory.');
                }
            }
            else
            {
                throw new InvalidArgumentException(
                    "Invalid directory specification : $path");
            }
        }
        else
        {
            throw new UC_PHPFileSystemException(
                "The directory : $path already exists.");
        }
    }
    
    public static function create_text_file($file_name, $overwrite = false)
    {
        if(is_file($file_name))
        {
            if($overwrite)
            {
                set_error_handler(array('UC_PHPFileSystem', 'error_handler'));
                unlink($file_name);
                restore_error_handler();
            }
            else
            {
                throw new InvalidArgumentException(
                    "The file $file_name already exists.");
            }
        }
        
        return new UC_PHPTextStream(
            $file_name, UC_PHPIOMode::FOR_WRITING, true);
    }
    
    public static function delete_file($file_spec)
    {
        if(is_file($file_spec))
        {
            $file = new UC_PHPFile($file_spec);
            $file->delete();
        }
        elseif(is_dir(pathinfo($file_spec, PATHINFO_DIRNAME)))
        {
            foreach(glob($file_spec) as $item)
            {
                if(is_file($item))
                {
                    $file1 = new UC_PHPFile($item);
                    $file1->delete();
                }
            }
        }
        else
        {
            throw new UC_PHPFileSystemException(
                "Unable to delete file : $file_spec");
        }
    }
    
    public static function delete_directory($directory_spec)
    {
        if(is_dir($directory_spec))
        {
            $dir = new UC_PHPDirectory($directory_spec);
            $dir->delete();
        }
        elseif(is_dir(pathinfo($directory_spec, PATHINFO_DIRNAME)))
        {
            foreach(glob($directory_spec) as $item)
            {
                if(is_dir($item))
                {
                    $dir1 = new UC_PHPDirectory($item);
                    $dir1->delete();
                }
            }
        }
        else
        {
            throw new UC_PHPFileSystemException(
                "Unable to delete directory: $directory_spec");
        }
    }
    
    public static function file_exists($file_spec)
    {
        return is_file($file_spec);            
    }
    
    public static function directory_exists($directory_spec)
    {
        return is_dir($directory_spec);
    }
    
    public static function get_absolute_path_name($path)
    {
        return realpath($path);
    }
    
    public static function get_base_name($path, $suffix = null)
    {
        return basename($path, $suffix);
    }
    
    public static function get_extension_name($path)
    {
        return pathinfo($path, PATHINFO_EXTENSION);
    }
    
    public static function get_file($file_path)
    {
        if($this->file_exists($file_path))
        {
            return new UC_PHPFile($file_path);
        }
        else
        {
            throw new InvalidArgumentException(
                "The path : $file_path doesn't exist.");
        }
    }
    
    public static function get_file_name($path)
    {
        return pathinfo($path, PATHINFO_FILENAME);
    }
    
    public static function get_directory($directory_path)
    {
        if($this->directory_exists($directory_path))
        {
            return new UC_PHPDirectory($directory_path);
        }
        else
        {
            throw new InvalidArgumentException(
                "The path : $directory_path doesn't exist.");
        }
    }
    
    public static function move_file($source, $destination)
    {
        
    }
    
    public static function move_directory($source, $destination)
    {
        
    }
    
    public static function open_text_file(
        $file_name, $mode = UC_PHPIOMode::FOR_READING, $create = false)
    {
        return new UC_PHPTextStream($file_name, $mode, $create);
    }
    
    private static function error_handler($errno, $errstr, $errfile, $errline)
    {
        if(strpos($errstr, 'Permission denied'))
        {
            $trc = debug_backtrace();
            
            if(isset($trc[2]['function']))
            {
                if($trc[2]['function'] == 'copy_file')
                {
                    throw new UC_PHPFileSystemException(
                        'Unable to copy file: permission denied' 
                        . ' in ' . $errfile . ' on line ' . $errline);
                }
                elseif($trc[2]['function'] == 'create_text_file')
                {
                    throw new UC_PHPFileSystemException(
                        'Unable to create text file: permission denied' 
                        . ' in ' . $errfile . ' on line ' . $errline);
                }
            }
        }
        
        return false;
    }
}

?>
